package com.goods.business.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ListMapper;
import com.goods.business.service.ListService;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author ：MJS
 * @date ：Created in 2021/6/8  15:27
 * @description：TODO
 */
@Service
@SuppressWarnings("all")
public class ListServiceImpl implements ListService {
    @Autowired(required = false)
    private ListMapper listMapper;

    //查询全部物资 及 模糊查询
    @Override
    public PageVO<ProductVO> findProductList(Integer pageNum, Integer pageSize, ProductVO productVO)throws BusinessException {
        //已经拥有的
        PageHelper.startPage(pageNum,pageSize);
        Example o = new Example(Product.class);
        String name = productVO.getName();
        Integer status = productVO.getStatus();

        Example.Criteria criteria = o.createCriteria();
        if(name!=null&&!"".equals(name)){
            criteria.andLike("name","%"+name+"%");
        }
        if(status!=null){
            criteria.andEqualTo("status",status);
        }
        if(null!=productVO.getCategorys()&&productVO.getCategorys().length==3){
                criteria.andEqualTo("threeCategoryId",productVO.getCategorys()[2]);
        }else if(null!=productVO.getCategorys()&&productVO.getCategorys().length!=3){
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"只支持三级分类查询");
        }
        List<Product> productList = listMapper.selectByExample(o);
        List<ProductVO> productVos = productList.stream().map(product -> {
            ProductVO productVO1 = new ProductVO();
            BeanUtils.copyProperties(product, productVO1);
            return productVO1;
        }).collect(Collectors.toList());

        PageInfo<Product> pageInfo = new PageInfo(productList);
        return new PageVO<>(pageInfo.getTotal(),productVos);
    }

    //物资添加
    @Transactional
    @Override
    public void add(ProductVO productVO) {
        //查询分类集合
        Long[] categorys = productVO.getCategoryKeys();
        //查询一二三级分类
        if(categorys.length>0 && categorys !=null){
            productVO.setOneCategoryId(categorys[0]);
            productVO.setTwoCategoryId(categorys[1]);
            productVO.setThreeCategoryId(categorys[2]);
        }

        //建实体类
        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        product.setCreateTime(new Date());
        product.setModifiedTime(new Date());
        String s = UUID.randomUUID().toString();
        product.setPNum(s);
        product.setStatus(Product.AUDIT);
        listMapper.insert(product);
    }
    //数据回显
    @Transactional
    @Override
    public ProductVO edit(Long id) throws BusinessException {
        Product product = new Product();
        product.setId(id);
        Product product1 = listMapper.selectOne(product);
        if(product1==null){
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"要编辑的物资不存在");
        }
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(product1,productVO);
        return productVO;
    }
    //更新数据
    @Override
    public void update(Long id, ProductVO productVO) {
        productVO.setModifiedTime(new Date());
        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        Example o = new Example(product.getClass());
        o.createCriteria().andEqualTo("id",id);
        listMapper.updateByExample(product,o);
    }
    //将数据移入回收站
    @Override
    public void remove(Long id) {
        Product product = new Product();
        product.setId(id);
        Product product1 = listMapper.selectOne(product);
        product1.setStatus(Product.DELETE);
        product1.setModifiedTime(new Date());
        Example o = new Example(product.getClass());
        o.createCriteria().andEqualTo("id",id);
        listMapper.updateByExample(product1,o);
    }
    //将数据移出回收站
    @Override
    public void back(Long id) {
        Product product = new Product();
        product.setId(id);
        Product product1 = listMapper.selectOne(product);
        product1.setStatus(Product.NORMAL);
        product1.setModifiedTime(new Date());
        Example o = new Example(product.getClass());
        o.createCriteria().andEqualTo("id",id);
        listMapper.updateByExample(product1,o);
    }
    //将数据永久删除
    @Override
    public void delete(Long id) {
        Product product=new Product();
        product.setId(id);
        Product product1 = listMapper.selectOne(product);
        if(product1.getStatus()==1){
            listMapper.delete(product);
        }
    }
    //数据审核
    @Override
    public void publish(Long id) {
        Product product = new Product();
        product.setId(id);
        Product product1 = listMapper.selectOne(product);
        product1.setStatus(Product.NORMAL);
        product1.setModifiedTime(new Date());
        Example o = new Example(product.getClass());
        o.createCriteria().andEqualTo("id",id);
        listMapper.updateByExample(product1,o);
    }
}
